package com.itheima.service.impl;


import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;

import com.itheima.utils.Md5Utils;
import com.itheima.utils.MyBatisUtils;

import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

    /**
     * 注册
     * @param user
     */
    @Override
    public void save(User user) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        try {
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            //1.密码应该加密的 不应该是明文的  使用的是密文数据
            String md5Pwd = Md5Utils.encodeByMd5(user.getPassword());
            user.setPassword(md5Pwd);
            //正常保存
            userDao.save(user);
        } finally {
            //释放资源
            MyBatisUtils.commitAndClose(sqlSession);
        }


    }



    /**
     * 登录方法实现
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);


        try {
            //加密处理
            String md5Pwd = Md5Utils.encodeByMd5(password);
            //登录   找到数据就有值 没找到数据就是null
            User user = userDao.findByUserNameAndPwd(username , md5Pwd);
            return user;
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }

    }



    /**
     * 根据用户ID查找数据
     * @param uid
     * @return
     */
    @Override
    public User findById(int uid) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = null ; //定义变量
        try {
            //查询用户数据
            user = userDao.findUserById(uid);
            //返回数据
            return user;
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }

    }

    /**
     * 修改用户数据
     * @param user
     */
    @Override
    public void updateUser(User user) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {
            userDao.updateUser(user);
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }
    }
    /**
     * 修改用户数据
     * @param user
     */
    @Override
    public void updatePic(User user) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {
            userDao.updatePic(user);
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }
    }

    /**
     * 查询用户地址数据
     * @param uid
     * @return
     *//*
    @Override
    public List<Address> findMyAddressByUid(int uid) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {

            //查询用户地址数据
            List<Address> addressList = userDao.findMyAddressByUid(uid);
            //返回
            return addressList;
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }
        return null;
    }*/


    /**
     * 模版代码
     */
    public void model(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {

        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
        finally {
            MyBatisUtils.commitAndClose(sqlSession);
        }
    }
}
